#include "i2c.h"
#include "oled.h"
#include "oled_font.h"
void _writeCMD(uint8_t cmd)
{   
    HAL_I2C_Mem_Write(&hi2c1, OLED_DEV_ADDRESS, OLED_CMD_ADDRESS, I2C_MEMADD_SIZE_8BIT ,&cmd, 1, 0x100);

}

void _writeDATA(uint8_t data)
{   
    HAL_I2C_Mem_Write(&hi2c1, OLED_DEV_ADDRESS, OLED_DATA_ADDRESS, I2C_MEMADD_SIZE_8BIT ,&data, 1, 0x100);
}

//设置光标位置
void _setPosition(uint8_t x, uint8_t page)
{
    _writeCMD(0x00 | (x & 0x0f));
    _writeCMD(0x10 | (x & 0xf0) >> 4);
    _writeCMD(0xB0 | page);
}
//外部函数
//初始化
void OLED_Init(void)
{
    uint8_t CMD_Data[]={
        0xae, 0xd5, 0x80, 0xa8, 0x3f, 0xd3, 0x00, 0x40, 0xa1, 0xc8, 0xda, 0x12, 0x81, 0xcf, 0xd9, 0xf1, 0xdb, 0x40, 0xa4, 0xa6, 0x8d, 0x14, 0xaf
    };
    for(uint8_t i = 0; i < 23; i++)
    {
        _writeCMD(CMD_Data[i]);
    }
}
//清屏
void OLED_Clear(void)
{
    for(uint8_t i=0; i < 128; i++)
    {
        for(uint8_t j=0; j < 8; j++)
        {
            _setPosition(i, j);
            _writeDATA(0x00);
        }
    }
}
//显示字符
void OLED_ShowChar8X16(uint8_t x, uint8_t page, char charIndex)
{
    uint8_t index = charIndex - ' ';
    _setPosition(x, page);
    for(uint8_t i = 0; i < 8; i++)
    {
        _writeDATA(char8X16[index * 2][i]);
    }
    _setPosition(x, page+1);
    for(uint8_t i = 0; i < 8; i++)
    {
        _writeDATA(char8X16[index * 2 + 1][i]);

    }

}

void OLED_ShowChar6X8(uint8_t x, uint8_t page, char charIndex)
{
    uint8_t index = charIndex - ' ';
    _setPosition(x, page);
    for(uint8_t i = 0; i < 6; i++)
    {
        _writeDATA(char6X8[index][i]);
    }
}

void OLED_ShowChinese16X16(uint8_t x, uint8_t page, uint8_t chineseIndex)
{
    _setPosition(x, page);
    for(uint8_t i = 0; i < 16; i++)
    {
        _writeDATA(font16X16[chineseIndex * 2][i]);
    }
    _setPosition(x, page+1);
    for(uint8_t i = 0; i < 16; i++)
    {
        _writeDATA(font16X16[chineseIndex * 2 + 1][i]);

    }
}
//显示字符串
void OLED_ShowString8X16(uint8_t x, uint8_t page, char* string)
{
    for(uint8_t index = 0; string[index] != '\0'; index++)
    {
        OLED_ShowChar8X16(x + index * 8, page, string[index]);
    }

}

void OLED_ShowString6X8(uint8_t x, uint8_t page, char* string)
{
    for(uint8_t index = 0; string[index] != '\0'; index++)
    {
        OLED_ShowChar6X8(x + index * 6, page, string[index]);
    }
}
//显示图像
void OLED_ShowIMG(uint8_t x, uint8_t page, uint8_t imgWeight, uint8_t imgHeight)
{
    for(uint8_t i = 0; i < imgHeight; i++)
    {
        _setPosition(x, page + i);
        for(uint8_t j = 0; j < imgWeight; j++)
        {
            _writeDATA(image2[i * imgWeight + j]);
        }
        
    }
}
//取数字位
void _getNumPost(uint32_t num, uint8_t length, uint8_t* array)
{
    for(uint8_t i = 0; i < length; i++)
    {
        array[length - i - 1] = num % 10;
        num /= 10;
    }
}

//显示数字
void OLED_ShowNum8X16(uint8_t x, uint8_t page, uint32_t num, uint8_t length)
{
    uint8_t numArray[length];
    _getNumPost(num, length, &numArray[0]);
    for(uint8_t i = 0; i < length; i++)
    {
        OLED_ShowChar8X16(x + i * 8, page, numArray[i]+'0');
    }
}
void OLED_ShowNum6X8(uint8_t x, uint8_t page, uint32_t num, uint8_t length)
{
    uint8_t numArray[length];
    _getNumPost(num, length, &numArray[0]);
    for(uint8_t i = 0; i < length; i++)
    {
        OLED_ShowChar6X8(x + i * 6, page, numArray[i]+'0');
    }
}
